package com.gage.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gage.commons.result.Tree;
import com.gage.model.shiro.ShiroUser;
import com.gage.mapper.SysResourceMapper;
import com.gage.mapper.SysRoleMapper;
import com.gage.mapper.SysRoleResourceMapper;
import com.gage.mapper.SysUserRoleMapper;
import com.gage.model.SysResource;
import com.gage.service.IResourceService;

/**
 *
 * Resource 表数据服务层接口实现类
 *
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysResource> implements IResourceService {
    private static final int RESOURCE_MENU = 0; // 菜单
    private static final String RESOURCE_MENU_ADMIN = "admin"; // admin为平台后台管理菜单,association社团管理平台菜单
    private static final String RESOURCE_MENU_ASSOCIATION = "admin";
    @Autowired
    private SysResourceMapper sysResourceMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleResourceMapper sysRoleResourceMapper;
    
    @Override
    public List<SysResource> selectAll() {
        EntityWrapper<SysResource> wrapper = new EntityWrapper<SysResource>();
        wrapper.orderBy("seq");
        return sysResourceMapper.selectList(wrapper);
    }
    
    public List<SysResource> selectByType(Integer type,String source) {
        EntityWrapper<SysResource> wrapper = new EntityWrapper<SysResource>();
        SysResource sysResource = new SysResource();
        wrapper.setEntity(sysResource);
        wrapper.addFilter("resource_type = {0}", type);
        wrapper.addFilter("resource_source = {0}", source);
        wrapper.orderBy("seq");
        return sysResourceMapper.selectList(wrapper);
    }
    
    @Override
    public List<Tree> selectAllMenu() {
        List<Tree> trees = new ArrayList<Tree>();
        // 查询所有菜单
        List<SysResource> sysResources = this.selectByType(RESOURCE_MENU,RESOURCE_MENU_ADMIN);
        if (sysResources == null) {
            return trees;
        }
        for (SysResource sysResource : sysResources) {
            Tree tree = new Tree();
            tree.setId(sysResource.getId());
            tree.setPid(sysResource.getPid());
            tree.setText(sysResource.getName());
            tree.setIconCls(sysResource.getIcon());
            tree.setAttributes(sysResource.getUrl());
            tree.setState(sysResource.getOpened());
            trees.add(tree);
        }
        return trees;
    }
    
    @Override
    public List<Tree> selectAllTree() {
        // 获取所有的资源 tree形式，展示
        List<Tree> trees = new ArrayList<Tree>();
        List<SysResource> sysResources = this.selectAll();
        if (sysResources == null) {
            return trees;
        }
        for (SysResource sysResource : sysResources) {
            Tree tree = new Tree();
            tree.setId(sysResource.getId());
            tree.setPid(sysResource.getPid());
            tree.setText(sysResource.getName());
            tree.setIconCls(sysResource.getIcon());
            tree.setAttributes(sysResource.getUrl());
            tree.setState(sysResource.getOpened());
            trees.add(tree);
        }
        return trees;
    }
    
    @Override
    public List<Tree> selectTree(ShiroUser shiroUser) {
        List<Tree> trees = new ArrayList<Tree>();
        // shiro中缓存的用户角色
        Set<String> roles = shiroUser.getRoles();
        if (roles == null) {
            return trees;
        }
        // 如果有超级管理员权限
        if (roles.contains("admin")) {
            List<SysResource> resourceList = this.selectByType(RESOURCE_MENU,RESOURCE_MENU_ADMIN);
            if (resourceList == null) {
                return trees;
            }
            for (SysResource sysResource : resourceList) {
                Tree tree = new Tree();
                tree.setId(sysResource.getId());
                tree.setPid(sysResource.getPid());
                tree.setText(sysResource.getName());
                tree.setIconCls(sysResource.getIcon());
                tree.setAttributes(sysResource.getUrl());
                tree.setOpenMode(sysResource.getOpenMode());
                tree.setState(sysResource.getOpened());
                trees.add(tree);
            }
            return trees;
        }
        // 普通用户
        List<Long> roleIdList = sysUserRoleMapper.selectRoleIdListByUserId(shiroUser.getId());
        if (roleIdList == null) {
            return trees;
        }
        List<SysResource> resourceLists = sysRoleMapper.selectResourceListByRoleIdList(roleIdList);
        if (resourceLists == null) {
            return trees;
        }
        for (SysResource sysResource : resourceLists) {
            Tree tree = new Tree();
            tree.setId(sysResource.getId());
            tree.setPid(sysResource.getPid());
            tree.setText(sysResource.getName());
            tree.setIconCls(sysResource.getIcon());
            tree.setAttributes(sysResource.getUrl());
            tree.setOpenMode(sysResource.getOpenMode());
            tree.setState(sysResource.getOpened());
            trees.add(tree);
        }
        return trees;
    }

	@Override
	public boolean deleteById(Serializable resourceId) {
		sysRoleResourceMapper.deleteByResourceId(resourceId);
		return super.deleteById(resourceId);
	}
}